﻿@using SelfMadeMillionaire.Core.Enums
@using SelfMadeMillionaire.Web.Infrastructure
@using SelfMadeMillionaire.Web.Infrastructure.Formatters
@using SelfMadeMillionaire.Web.Infrastructure.ViewModels.Publications
@using SelfMadeMillionaire.Web.Infrastructure.ViewModels.Home
@using PagedList.Mvc
@model PublicationAndNewsListViewModel

	<div class="table-responsive">
		@if (Model.Publications.Any())
  {
			<table class="table table-bordered tablesorter">
				<thead>
					<tr>
						<th width="20%"><a href="#">Date</a></th>
						<th width="80%"><a href="#">Title</a></th>
					</tr>
				</thead>
				<tbody>
                    @foreach (MixedPublicationItemModel item in Model.Publications )
		            {
                        <tr>
							<td>@item.Date.ToString("MM/dd/yyyy")</td>
							<td>
                                @if (item.IsPublication)
								{
									if (String.IsNullOrEmpty(item.ExternalUrl))
									{
										<a href="@Url.Action("Details", "Publications", new { id = item.ID })">@item.Title</a>
									}
									else
									{
										<a href="@item.ExternalUrl" target="blank">@item.Title</a>
									}
								}
								else if (item.IsNews)
								{
                                    <a href="@item.ExternalUrl" target="_blank" title="@item.Tooltip">@item.Title</a>
                                }

							</td>
						</tr>
		            }
				</tbody>
			</table>
	  @Html.PagedListPager(Model.Publications, page => Url.Action("Index", new { page, startDate = Model.StartDate, endDate = Model.EndDate, selectedProduct = Model.ProductTypes.SelectedValue, title = Model.Title, sort = Model.SortOrder }))
  }
  else
  {
			<p>No records found.</p>
  }
	</div>

<script type="text/javascript">
	$(function () {
	    $("#publications-list table").tablesorter();

	    var sortRefresh = true;
        @if(!string.IsNullOrEmpty(Model.SortOrder))
        {
            <text>
	        sortRefresh = false;
	        setInitSort("publications-list", '@(Model.SortOrder)');
            </text>
        }

	    $("#publications-list table").on("sortEnd", function (event) {
	        $("#SortOrder").val(event.target.config.sortList);
	        if (sortRefresh)
	            RefreshList();
	        else
	            sortRefresh = true;

	    });
	});
</script>